<template>
  <div style="padding: 20px;">
    <div class="topLocation">球员信息管理 / 球员管理</div>
    <div class="topTool">
      <div class="group">球员姓名：</div>
      <el-input v-model="searchName" placeholder="请输入球员姓名" style="margin-right: 50px;"></el-input>
      <div class="group">球员用户名：</div>
      <el-input v-model="searchUsername" placeholder="请输入球员用户名" style="margin-right: 50px;"></el-input>
      <div class="group">球员司职位置：</div>
      <el-select v-model="searchPosition" placeholder="请选择球员司职位置">
        <el-option label="控球后卫" value="控球后卫"></el-option>
        <el-option label="得分后卫" value="得分后卫"></el-option>
        <el-option label="小前锋" value="小前锋"></el-option>
        <el-option label="大前锋" value="大前锋"></el-option>
        <el-option label="中锋" value="中锋"></el-option>
        <el-option label="所有位置" value=""></el-option>
      </el-select>
      <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
    </div>
    <el-table
        :data="playerData"
        :header-cell-style="{ background: '#f6f9fa'}"
        @sort-change="handleSortChange"
        empty-text="暂无数据">
      <el-table-column type="selection"/>
      <el-table-column prop="id" label="ID" sortable></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column prop="position" label="司职位置"></el-table-column>
      <el-table-column prop="phonenumber" label="手机号码"></el-table-column>
      <el-table-column prop="addtime" label="参加时间"></el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
        background
        v-model:currentPage="currentPage"
        v-model:pageSize="pageSize"
        :page-sizes="[5, 10, 20]"
        :total="total"
        layout="prev, pager, next, sizes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import {computed, onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage, ElMessageBox} from "element-plus";
import {Search} from "@element-plus/icons-vue";

const playerData = ref([]); // 球员数据
const currentPage = ref(1); // 当前页数
const pageSize = ref(5); // 每页显示条数
const total = ref(0); // 总记录数
const searchName = ref(''); // 球员姓名搜索关键字
const searchUsername = ref(''); // 球员用户名搜索关键字
const searchPosition = ref(''); // 球员司职位置搜索关键字

const formatDate = (row, column, cellValue) => {
  if (!cellValue) return '无日期';
  const date = new Date(cellValue);
  if (isNaN(date.getTime())) {
    return '无效日期';
  }
  return date.toLocaleDateString();
};

// 组件挂载时加载数据
onMounted(() => {
  getData();
});

// 获取数据
const getData = () => {
  axios.get('http://localhost:8080/player/findByPage', {
    params: {
      pageNum: currentPage.value,
      pageSize: pageSize.value,
      name: searchName.value,
      username: searchUsername.value,
      position: searchPosition.value
    }
  }).then(response => {
    playerData.value = response.data.data.records;
    total.value = response.data.data.total;
  }).catch(error => {
    console.error(error);
  });
};

const handleCurrentChange = (pageNum) => {
  currentPage.value = pageNum;
  getData();
};

const handleSizeChange = (size) => {
  pageSize.value = size;
  getData();
};

// 查询操作
const handleSearch = () => {
  const params = {
    name: searchName.value || null,
    username: searchUsername.value || null,
    position: searchPosition.value || null
  };
  axios.get('http://localhost:8080/player/search', { params })
      .then(response => {
        playerData.value = response.data.data;
        total.value= response.data.data.length; // 假设返回的是一个数组，直接取长度
      })
      .catch(error => {
        console.error(error);
        ElMessage.error('查询失败');
      });
};

// 字段排序
const handleSortChange = (sortData) => {
  const { prop, order } = sortData;
  playerData.value = playerData.value.sort((a, b) => {
    if (order === 'ascending') {
      return a[prop] > b[prop] ? 1 : -1;
    } else {
      return a[prop] < b[prop] ? 1 : -1;
    }
  });
};
</script>

<style scoped>
.topLocation {
  font-size: 16px;
  font-weight: bold;
  color: #000; /* 文字颜色为黑色 */
  padding: 10px;
  border-bottom: 2px solid #000; /* 底部边框，黑色 */
  display: flex;
  align-items: center; /* 垂直居中对齐 */
  justify-content: flex-start; /* 左对齐 */
  margin-bottom: 20px; /* 增加距离 */
}
.topTool {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.el-pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.group {
  white-space: nowrap; /* 防止文字换行 */
  font-size: 14px;     /* 设置字体大小为 14px */
}
</style>

